package com.example.baselibrary.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Vibrator;

/**
 * 硬件处理
 *
 * @author: NPF
 * @date: 2018/4/13.
 */
public class LKSystemHardUtil {


    private static LKSystemHardUtil mInstance;
    private static long mLastNotifiyTime;
    private AudioManager mAudioManager;
    private Vibrator mVibrator;
    private Ringtone mRingtone = null;

    private LKSystemHardUtil() {
    }

    public static LKSystemHardUtil getInstance() {
        if (null == mInstance) {
            synchronized (LKSystemHardUtil.class) {
                if (null == mInstance) {
                    mInstance = new LKSystemHardUtil();
                }
            }
        }
        return mInstance;
    }


    /**
     * 震动和声音
     */
    @SuppressLint("MissingPermission")
    public void vibrateAndPlayTone(Context context, boolean isVibrate, boolean isPlayTone) {
        if (System.currentTimeMillis() - mLastNotifiyTime < 5000) {//2秒内收到新消息,跳过播放铃声
            return;
        }
        try {
            if (null == mAudioManager) {
                mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            }
            if (null == mVibrator) {
                mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            }
            mLastNotifiyTime = System.currentTimeMillis();
            if (mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
                return;
            }
            if (isVibrate) {//震动
                long[] pattern = new long[]{0, 180, 80, 120};
                mVibrator.vibrate(pattern, -1);
            }
            if (isPlayTone) {//铃声
                if (mRingtone == null) {//TYPE_NOTIFICATION
                    Uri notificationUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                    mRingtone = RingtoneManager.getRingtone(context, notificationUri);
                    if (mRingtone == null) {
                        return;
                    }
                }
                if (!mRingtone.isPlaying()) {
                    String vendor = Build.MANUFACTURER;
                    mRingtone.play();
                    if (vendor != null && vendor.toLowerCase().contains("samsung")) {
                        Thread ctlThread = new Thread() {
                            public void run() {
                                try {
                                    if (mRingtone.isPlaying()) {
                                        mRingtone.stop();
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        };
                        ctlThread.run();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
